package com.aaa.hospital.ManagementEnd.controller;

import com.aaa.hospital.Client.pojo.Department;
import com.aaa.hospital.Client.service.Impl.DepartmentServiceImpl;
import com.aaa.hospital.ManagementEnd.pojo.Docuser;
import com.aaa.hospital.ManagementEnd.service.Impl.DocuserServiceImpl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

// 测试更新
//医生页面
@Controller
@RequestMapping("/Docuser")
public class DocuserController {
    @Autowired
    private DocuserServiceImpl docuserServiceImpl;
    @Autowired
    private DepartmentServiceImpl departmentServiceImpl;

    @RequestMapping(value = "/selectAll",method = RequestMethod.GET)
    // 将java对象转为json格式的数据
    @ResponseBody
    //查询医生信息
    public String selectAll(){
        JSONObject json = new JSONObject();
        List<Docuser> data = docuserServiceImpl.queryAll();
        Object objData=JSONObject.toJSON(data);
        json.put("code", 0);
        json.put("msg", "");
       json.put("count",data.size());
        json.put("data",objData);
        return json.toJSONString();
    }
    //添加
    @RequestMapping(value = "/insert",method = RequestMethod.GET)
    @ResponseBody
    private Map<String, Object> insert(Docuser docuser) {
        int result =docuserServiceImpl.insert(docuser);
        Map<String, Object> map = new HashMap<>();
        if (result > 0) {
            map.put("status", 1);
        } else {
            map.put("status", 0);
        }
        return map;
    }
    //删除
    @RequestMapping(value = "/{did}", method = RequestMethod.GET)
    @ResponseBody
    //@PathVariable注解绑定它传过来的值到方法的参数上。
    private Map<String, Object> delete(@PathVariable("did") Integer did) {
        int result = docuserServiceImpl.delete(did);
        Map<String, Object> map = new HashMap<>();
        if (result > 0) {
            map.put("status", 1);
        } else {
            map.put("status", 0);
        }
        return map;
    }
    //修改
    @RequestMapping(value = "/update",method = RequestMethod.GET)
    @ResponseBody
    private Map<String, Object> update(Docuser docuser) {
        int result = docuserServiceImpl.update(docuser);
        Map<String, Object> map = new HashMap<>();
        if (result > 0) {
            map.put("status", 1);
        } else {
            map.put("status", 0);
        }
        return map;
    }
    //搜索框查询
    @RequestMapping(name="根据輸入内容查询信息",value = "/select_did_Name",method = RequestMethod.GET)
    @ResponseBody
    public String selectName(@Param("did") Integer did,@Param("name") String name){
        JSONObject json = new JSONObject();
        List<Docuser> data =docuserServiceImpl.select_did_Name(did,name);
        Object objData=JSONObject.toJSON(data);
        json.put("code", 0);
        json.put("msg", "");
        json.put("count",data.size());
        json.put("data",objData);
        return json.toJSONString();
    }
    //查询医生科室类型
    @RequestMapping(value = "/SelectType",produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String SelectType(){
        List<Department> list =departmentServiceImpl.queryAll();
        return JSON.toJSONString(list);
    }

}
